package Server5;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
    private ServerSocket serverSocket;
    private List<PrintWriter> allOut=new ArrayList<>();


    private Server(){
        try{
            serverSocket=new ServerSocket(8088);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void start(){
        try {
            while (true){
                System.out.println("等待链接");
                Socket socket=serverSocket.accept();
                System.out.println("链接成功");
                //启动线程
                ClientHandler handler=new ClientHandler(socket);

                Thread h=new Thread(handler);
                h.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server=new Server();
        server.start();
    }

    private class ClientHandler implements Runnable{
        private Socket socket;
        private String host;

        public ClientHandler(Socket socket){
            this.socket=socket;
            this.host=socket.getInetAddress().getHostAddress();//获取id，就是ip地址
        }
        public void run(){
            PrintWriter pw=null;
            try {
                InputStream is=socket.getInputStream();
                InputStreamReader isr=new InputStreamReader(is,"UTF-8");
                BufferedReader br=new BufferedReader(isr);

                OutputStream os=socket.getOutputStream();
                OutputStreamWriter ow=new OutputStreamWriter(os,"UTF-8");
                BufferedWriter bw=new BufferedWriter(ow);
                pw=new PrintWriter(bw,true);

                //将输出流存入集合中
                synchronized (allOut){
                    allOut.add(pw);
                }

                sendMessage(host+"我上线了"+",当前在线人数："+allOut.size());

                while (true){
                    sendMessage(host+"我上线了"+"只因你太美，oh，baby");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                /*String message;
                while ((message= br.readLine())!=null){
                    sendMessage(host+"说"+message);
                }*/
            } catch (IOException e) {
                //e.printStackTrace();
            }finally {
                sendMessage(host+"离线了");
                synchronized (allOut){
                    allOut.remove(pw);
                }
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        private void sendMessage(String message){
            System.out.println(message);
            //迭代器要求遍历过程中不能用集合分方法增删元素，所以要和增删互斥锁
            synchronized (allOut){
                for (PrintWriter o:allOut){
                    o.println(message);
                }
            }

        }
    }
}
